package com.haisen.mr;

import lombok.extern.slf4j.Slf4j;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
@Slf4j
public class MyReduce extends Reducer<Text,IntWritable,Text,IntWritable> {


       private IntWritable result = new IntWritable();

       //相同的key为一组...,调用一次reduce方法，在了方法内迭代这一组数据，
       //进行计算：sum,count,max,min....

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //super.reduce(key, values, context);

        //hello 1
        //hello 1

        //key: hello
        //values:(1,1,1,1)
        int sum =0;
        for (IntWritable val : values){
            sum += val.get();
        }
        log.info("sum={}",sum);
        result.set(sum);
        context.write(key,result);
    }

}
